﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Extensions;

namespace GTAuto.DAL.DAL
{
    public class DALTireDiscs : DALBase
    {
        public static DALTireDiscs Singleton = new DALTireDiscs();

        public List<GTModelModification> TireSearch(string shirina, string diameter, string profile, string seson, string shipovka, int? brandId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                
                var brands = db.TireSearch(shirina,   diameter, profile,   seson,   shipovka, brandId).ToList();
                
                var ret = new List<GTModelModification>();
                foreach (var brand in brands)
                {
                    GTModelModification gtBrand = new GTModelModification();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }

        public List<GTModel> TireModelsSearch(string shirina, string diameter, string profile, string seson, string shipovka, int? brandId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {


                var modifications = db.TireSearch(shirina, diameter, profile, seson, shipovka, brandId).ToList();

                var temp = new List<Model>();

                foreach (var brand in modifications)
                {
                    var tp = temp.SingleOrDefault(x => x.Id == brand.ModelId);

                    if(tp==null)
                    {
                        temp.Add(brand.Model);
                    }
                }


                var ret = new List<GTModel>();
                foreach (var model in temp)
                {
                     GTModel gtModel = new GTModel();
                     gtModel.UpdateTo(model);
                     ret.Add(gtModel);
                }

                return ret;
            }
        }



        public List<GTModelModification> DiscsSearch( string diameter, string mezbolt,string otv, string vilet,  int? brandId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {


                var brands = db.DiscsSearch(diameter,  mezbolt, otv,  vilet,  brandId).ToList();

                var ret = new List<GTModelModification>();
                foreach (var brand in brands)
                {
                    GTModelModification gtBrand = new GTModelModification();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }


    }
}
